package com.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.domain.IssueCommentResponse;
import com.domain.UserLikeWithSonComment;
import com.mapper.helping.UserLikeWithSonCommentMapper;
import com.mapper.helping.IssueCommentResponseMapper;
import com.service.IUserLikeWithSonCommentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.util.UserUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 大忽悠
 * @since 2022年02月15日
 */
@RequiredArgsConstructor
@Service
@Transactional(value = "primaryTransactionManager")
public class UserLikeWithSonCommentServiceImpl extends ServiceImpl<UserLikeWithSonCommentMapper, UserLikeWithSonComment> implements IUserLikeWithSonCommentService {
  private final IssueCommentResponseMapper issueCommentResponseMapper;
  /**
     * <p>
     *     不能重复点赞
     * </p>
     * @param son_comment_id 儿子评论id
     * @return
     */
    @Override
    public boolean likeTargetSonComment(Integer son_comment_id) {
        IssueCommentResponse issueCommentResponse = issueCommentResponseMapper.selectById(son_comment_id);
        Assert.notNull(issueCommentResponse,"儿子评论不存在");
        UserLikeWithSonComment one = getOne(new QueryWrapper<UserLikeWithSonComment>()
                .eq("user_id", UserUtil.getUserId())
                .eq("son_comment_id", son_comment_id));
        Assert.isNull(one,"重复点赞");
        boolean save = save(UserLikeWithSonComment.builder().sonCommentId(son_comment_id).userId(UserUtil.getUserId()).build());
        //对于儿子评论的点赞总数加一
        issueCommentResponse.setLikeSum(issueCommentResponse.getLikeSum()+1);
        issueCommentResponseMapper.updateById(issueCommentResponse);
        return save;
    }

    @Override
    public boolean unLikeTargetSonComment(Integer son_comment_id) {
        IssueCommentResponse issueCommentResponse = issueCommentResponseMapper.selectById(son_comment_id);
        Assert.notNull(issueCommentResponse,"儿子评论不存在");
        UserLikeWithSonComment one = getOne(new QueryWrapper<UserLikeWithSonComment>()
                .eq("user_id", UserUtil.getUserId())
                .eq("son_comment_id", son_comment_id));
        Assert.notNull(one,"还未点赞");
        boolean remove = remove(new QueryWrapper<UserLikeWithSonComment>().eq("user_id", UserUtil.getUserId())
                .eq("son_comment_id", son_comment_id));
        //对于儿子评论的点赞总数减去一,这里不需要判断点赞数是否会小于0,,因为上面还未点赞已经暗中决定了
        issueCommentResponse.setLikeSum(issueCommentResponse.getLikeSum()-1);
        issueCommentResponseMapper.updateById(issueCommentResponse);
        return remove;
    }

}
